"""
@File    :   router.py
@Time    :   2024/03/21 16:19:31
@Author  :   Qrj
@Email   :   923207736@qq.com
@description   :   登录验证码相关路由
"""

from typing import Annotated

from fastapi import APIRouter, Depends

from src.business.admin.system.oauth2.auth.captcha.dependencies.route_depend import (
    RouteDependRandomGetImageCaptcha,
    RouteDependRandomGetSlidingCaptcha,
    route_depend_random_get_image_captcha,
    route_depend_random_get_sliding_captcha,
    route_depend_verify_image_captcha,
    route_depend_verify_sliding_captcha,
)
from src.business.admin.system.oauth2.auth.captcha.enums import CaptchaTypeEnum
from src.config import get_app_settings
from src.utils.unified_response import SuccessApiJSONResponse

# 获取应用配置
settings = get_app_settings()

# 创建路由
router = APIRouter(
    prefix="/admin/system/oauth2/auth/captcha",
    tags=[
        "后台验证码接口",
    ],
)

# 根据是否开启验证码来解锁对应的路由
if settings.enable_user_login_captcha:
    if settings.user_login_captcha_type == CaptchaTypeEnum.image:

        @router.get("/get", summary="获取图像验证码")
        async def route_get_image_captcha(
            image_captcha_info: Annotated[
                RouteDependRandomGetImageCaptcha,
                Depends(route_depend_random_get_image_captcha),
            ],
        ):
            data = {
                "code_id": image_captcha_info.code_id,
                "image_captcha_base64_str": image_captcha_info.images_base64_str,
            }
            return SuccessApiJSONResponse(message="获取图像验证码成功", data=data)

        @router.post("/verify", summary="验证图像验证码")
        async def route_verify_image_captcha(
            verify_result: Annotated[bool, Depends(route_depend_verify_image_captcha)],
        ):
            return SuccessApiJSONResponse(message="图像验证码验证成功")

    elif settings.user_login_captcha_type == CaptchaTypeEnum.sliding_puzzle:

        @router.get("/get", summary="获取滑块验证码")
        async def route_get_sliding_captcha(
            sliding_captcha_info: Annotated[
                RouteDependRandomGetSlidingCaptcha,
                Depends(route_depend_random_get_sliding_captcha),
            ],
        ):
            data = {
                "code_id": sliding_captcha_info.code_id,
                "base_img_base64_str": sliding_captcha_info.base_img_base64_str,  # 底图
                "slide_base64_str": sliding_captcha_info.slide_base64_str,  # 滑块图
            }
            return SuccessApiJSONResponse(message="获取滑块验证码成功", data=data)

        @router.post("/verify", summary="验证滑块验证码")
        async def route_verify_sliding_captcha(
            verify_result: Annotated[
                bool, Depends(route_depend_verify_sliding_captcha)
            ],
        ):
            return SuccessApiJSONResponse(message="滑块验证码验证成功")
